package be.kuleuven.thesis.pages;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import be.kuleuven.thesis.R;
import be.kuleuven.thesis.framework.Song;
import be.kuleuven.thesis.log.Logger;
import be.kuleuven.thesis.status.Player;
import be.kuleuven.thesis.status.PlaylistGenerator;
import be.kuleuven.thesis.status.StatusListener;
import be.kuleuven.thesis.widget.PlaylistAdapter;

public class CurrentPlaylist extends Activity implements StatusListener{
	public static final int CLEAR_PLAYLIST = Menu.FIRST;
	public static final int SEND_USER_DATA = Menu.FIRST+1;

	private ArrayAdapter<Song> songAdapter;
	private ListView playlist;
	private Player player;
	private PlaylistGenerator generator;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.current_playlist);

		player = Player.getInstance();
		generator = PlaylistGenerator.getInstance();
		prepareFilterButton();
		

		playlist = (ListView)findViewById(R.id.playlist_list);
		songAdapter = new PlaylistAdapter(this, Player.getInstance().getCurrentPlaylist());
		playlist.setAdapter(songAdapter);
	}

	@Override
	public void onResume() {
		super.onResume();
		setRadioButton();
		songAdapter.notifyDataSetChanged();
		playlist.setSelection(player.getCurrentIndex()-3);
		checkPlaylistIsEmpty();
		handleNumberOfSongsToGenerate();
		Player.getInstance().registerStatusListener(this);
		generator.registerStatusListener(this);
	}

	@Override
	public void onPause() {
		super.onPause();
		Player.getInstance().registerStatusListener(this);
		generator.unRegisterStatusListener(this);
	}

	

	public void scrollToBottom() {
		playlist.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
		playlist.setStackFromBottom(true);
	}

	public ArrayAdapter<Song> getSongAdapter() {
		return songAdapter;
	}

	private void setRadioButton() {
		ToggleButton radioButton = (ToggleButton) findViewById(R.id.radio_button);
		if(generator.isRadioOn() && !radioButton.isChecked()) {
			radioButton.setChecked(true);
		} else if(!generator.isRadioOn() && radioButton.isChecked()) {
			radioButton.setChecked(false);
		}
		radioButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				if(generator.isRadioOn()) {
					generator.turnRadioOff();
				} else {
					generator.turnRadioOn();
				}
				songAdapter.notifyDataSetChanged();
			}
		});
	}

	private void prepareFilterButton() {
		Button filterButton = (Button) findViewById(R.id.radio_filters);
		filterButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
//				Intent i = new Intent(CurrentPlaylist.this, SetRadioFilters.class);
//				View view = CurrentPlaylistGroup.group.getLocalActivityManager()
//						.startActivity("Set Radio Filters", i
//								.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
//								.getDecorView();
//				CurrentPlaylistGroup.group.replaceView(view);
				CurrentPlaylistGroup.group.loadActivity(CurrentPlaylistGroup.FILTERS_ID);
			}
		});
	}
	
	private void handleNumberOfSongsToGenerate() {
		RelativeLayout generationLayout = (RelativeLayout) findViewById(R.id.generation_layout);
		if(generator.isRadioOn())
			generationLayout.setVisibility(View.VISIBLE);
		else generationLayout.setVisibility(View.GONE);
		
		updateGenerationLabel();
		checkGenerationButtonAvailability();
		
		Button generationDown = (Button) findViewById(R.id.generation_down);
		generationDown.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				generator.decrementNbOfSongsToGenerate();
				updateGenerationLabel();
				checkGenerationButtonAvailability();
			}
		});
		
		Button generationUp = (Button) findViewById(R.id.generation_up);
		generationUp.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				generator.incrementNbOfSongsToGenerate();
				updateGenerationLabel();
				checkGenerationButtonAvailability();
			}
		});
	}
	
	private void updateGenerationLabel() {
		TextView generationLabel = (TextView) findViewById(R.id.number_of_songs_label);
		generationLabel.setText(": "+generator.getNbOfSongsToGenerate()+" ");
	}
	
	private void checkGenerationButtonAvailability() {
		Button generationDown = (Button) findViewById(R.id.generation_down);
		Button generationUp = (Button) findViewById(R.id.generation_up);
		
		//System.out.println(generator.getNbOfSongsToGenerate());
		switch(generator.getNbOfSongsToGenerate()) {
		case 1:
			generationDown.setBackgroundResource(R.drawable.timepicker_down_disabled);
			generationUp.setBackgroundResource(R.drawable.generation_up);
			break;
		case PlaylistGenerator.MAX_NB_SONGS_TO_GENERATE:
			generationDown.setBackgroundResource(R.drawable.generation_down);
			generationUp.setBackgroundResource(R.drawable.timepicker_up_disabled);
			break;
		default:
			generationDown.setBackgroundResource(R.drawable.generation_down);
			generationUp.setBackgroundResource(R.drawable.generation_up);
		}
		
	}

	private void checkPlaylistIsEmpty() {
		TextView emptyPlaylistText = (TextView) findViewById(R.id.empty_playlist);
		if(Player.getInstance().getCurrentPlaylist().isEmpty()) {
			emptyPlaylistText.setVisibility(TextView.VISIBLE);
		} else {
			emptyPlaylistText.setVisibility(TextView.GONE);
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		//super.onCreateOptionsMenu(menu);
		menu.clear();
		menu.add(0, CLEAR_PLAYLIST,0, R.string.menu_clear_playlist);
		menu.add(0, SEND_USER_DATA,0, "Send user data");
		return true;
	}
	
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch(item.getItemId()) {
		case CLEAR_PLAYLIST:
			Logger.getInstance().log("Playl", "Playlist Cleared", null);
			Player.getInstance().clearPlaylist();
			return true;
		case SEND_USER_DATA:
			Logger.getInstance().closeFile();
			Logger.getInstance().sendFile(this);
			return true;
		}
		return true;
		//return super.onMenuItemSelected(featureId, item);
	}

	@Override
	public void onCurrentSongChanged() {
		checkPlaylistIsEmpty();
		getSongAdapter().notifyDataSetChanged();
	}

	@Override
	public void onSongGenerated() {
		scrollToBottom();
		checkPlaylistIsEmpty();
		getSongAdapter().notifyDataSetChanged();
	}

	@Override
	public void onSongRemoved() {
		checkPlaylistIsEmpty();
		getSongAdapter().notifyDataSetChanged();		
	}

	@Override
	public void onPlaylistCleared() {
		checkPlaylistIsEmpty();
		getSongAdapter().notifyDataSetChanged();
	}

	@Override
	public void onRadioSwitched() {
		handleNumberOfSongsToGenerate();
	}

	@Override
	public void onMediaPlayed() {
		
	}

	@Override
	public void onBufferingUpdate(MediaPlayer mp, int percent) {
		
	}

	@Override
	public void onNetworkProblem() {
		setRadioButton();
		Toast.makeText(getApplicationContext(), getResources().getString(R.string.no_internet), Toast.LENGTH_LONG).show();
	}

	@Override
	public void onFiltersTooTight() {
		Toast.makeText(getApplicationContext(), getResources().getString(R.string.filters_severe), Toast.LENGTH_LONG).show();		
	}

	@Override
	public void onSongAdded() {
		getSongAdapter().notifyDataSetChanged();
	}
}